package com.xoa.service.party;


import com.xoa.dao.party.CommunityWjfxxMapper;
import com.xoa.model.party.CommunityWjfxx;
import com.xoa.util.DateFormat;
import com.xoa.util.ExcelUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.Transient;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommunityWjfxxService {

    @Autowired
    private CommunityWjfxxMapper communityWjfxxMapper;

    /**
     * 查询
     * @return
     */
    public ToJson select(HttpServletRequest request, CommunityWjfxx communityWjfxx,
                         Integer page, Integer pageSize, boolean useFlag, String export, HttpServletResponse response){
        ToJson json=new ToJson();
        Map<String,Object> map=new HashMap<String,Object>();
        PageParams pageParams = new PageParams();
        try{
//            map.put("wjfxxId",communityWjfxx.getWjfxxId());
//            map.put("wjfxxName",communityWjfxx.getWjfxxName());
//            map.put("page",page);
//            map.put("pageSize",pageSize);
            pageParams.setPageSize(pageSize);
            pageParams.setPage(page);
            map.put("pageParams",pageParams);
            map.put("communityWjfxx",communityWjfxx);
            List<CommunityWjfxx> lxdwxxcjs=communityWjfxxMapper.selectWjfxx(map);
            //time转换
            for(CommunityWjfxx communityWjfxx1:lxdwxxcjs){
                if(communityWjfxx1.getStartTime()!=null &&!"".equals(communityWjfxx1.getStartTime())){
                    Date d=communityWjfxx1.getStartTime();
                    String time= DateFormat.getStrDate(communityWjfxx1.getStartTime());
                    communityWjfxx1.setTime(time);
                }
            }

         //   int count=communityWjfxxMapper.selectWjfxxCount(communityWjfxx.getWjfxxName());
            if(export==null){
                export="0";
            }
            if (export.equals("1")){
                if (lxdwxxcjs !=null && lxdwxxcjs.size()>0) {
                    for (CommunityWjfxx list : lxdwxxcjs) {
                        if (list.getWjfxxLx() != null) {
                            if (1 == list.getWjfxxLx()) {
                                list.setType("摄像头");
                            } else if (2 == list.getWjfxxLx()) {
                                list.setType("门禁");
                            } else if (3 == list.getWjfxxLx()) {
                                list.setType("围栏");
                            } else if (4 == list.getWjfxxLx()) {
                                list.setType("其他");
                            } else {
                                list.setType("");
                            }
                        }
                        if (list.getStartTime()!=null){
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            String str = format.format(list.getStartTime());
                            list.setTime(str);
                        }
                        if (list.getYunzt()!=null){
                            if (1 == list.getYunzt()) {
                                list.setStatus("使用中");
                            } else if (2 == list.getYunzt()) {
                                list.setStatus("损坏");
                            } else if (3 == list.getYunzt()) {
                                list.setStatus("闲置");
                            } else {
                                list.setStatus("");
                            }
                        }
                    }
                }

                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("物技防信息采集", 9);
                String[] secondTitles = {"物技防名称", "物技防类型","编号", "安装日期","运行状态",
                        "详细地址","所属单位","负责人","负责人联系方式"};
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                String[] beanProperty = {"wjfxxName", "type","xmait", "time", "status",
                        "addresss","companyName","person","personTel"};
                HSSFWorkbook workbook = null;
                workbook = ExcelUtil.exportExcelData(workbook2, lxdwxxcjs, beanProperty);
                OutputStream out = response.getOutputStream();
                String filename = "物技防信息采集信息记录表.xls";
                filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setHeader("content-disposition", "attachment;filename=" + filename);
                workbook.write(out);
                out.close();
            }
            json.setObj(lxdwxxcjs);
            json.setTotleNum(pageParams.getTotal());
            json.setMsg("ok");
            json.setFlag(0);
            return json;
        }catch (Exception e){
            json.setMsg("ok");
            json.setFlag(0);
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 修改
     * @return
     */
    @Transient
    public ToJson update(HttpServletRequest request, CommunityWjfxx communityWjfxx){
        ToJson json=new ToJson();
        try{
            int count=communityWjfxxMapper.updateByPrimaryKey(communityWjfxx);
            if(count>0){
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }
            json.setFlag(0);
            json.setMsg("修改失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 删除
     * @return
     */
    @Transient
    public ToJson delete(Integer wjfxxId){
        ToJson json=new ToJson();
        try{
            int count=communityWjfxxMapper.deleteByPrimaryKey(wjfxxId);
            if(count>0){
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }
            json.setFlag(0);
            json.setMsg("删除失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 添加
     * @return
     */
    @Transient
    public ToJson add(HttpServletRequest request, CommunityWjfxx communityWjfxx){
        ToJson json=new ToJson();
        try{
            int count=communityWjfxxMapper.insert(communityWjfxx);
            if(count>0){
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }
            json.setFlag(0);
            json.setMsg("添加失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }
}
